package com.hystrix.command;

import com.alibaba.fastjson.JSONObject;
import com.hystrix.http.HttpPost;
import com.hystrix.model.InfoA;
import com.hystrix.util.Constant;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.HystrixThreadPoolProperties;

/**
 * 泛型为最终返回的数据结果
 * 
 * GetServiceAInfoCommand的线程池默认最多10个线程
 * 
 * @author wangxh
 *
 */
public class ServiceAMaxThreadCommand extends HystrixCommand<InfoA> {

	private int num;

	/**
	 * 手动设置线程池大小及请求超时时间
	 * 
	 * @param num
	 */
	public ServiceAMaxThreadCommand(int num) {
		super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("GetServiceAInfoMaxThreadCommand"))
				.andCommandPropertiesDefaults(
						HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(30000))
				.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.defaultSetter().withCoreSize(4)));
		this.num = num;
	}

	@Override
	protected InfoA run() throws Exception {
		System.out.println("这是第 " + num + " 个请求开始");
		String a = HttpPost.httpPost(Constant.serviceMaxThread, "", "utf-8");
		System.out.println("这是第 " + num + " 个请求结束");
		return JSONObject.parseObject(a, InfoA.class);
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

}
